package IEFYP.JunkCallFiltering;


import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;

public class ActionOnCalls extends PreferenceActivity implements OnSharedPreferenceChangeListener {
	CheckBoxPreference blacklist;
	CheckBoxPreference userlist;
	CheckBoxPreference start3;
	CheckBoxPreference start2;
	CheckBoxPreference no_num;
	CheckBoxPreference duration;
	
	
	ListPreference junkcall;
	ListPreference suspcall;
	
	 SharedPreferences myprefs;
	   @Override
	    protected void onCreate(Bundle savedInstanceState) {
	            super.onCreate(savedInstanceState);

	            addPreferencesFromResource(R.xml.actionpref);

	            getPreferenceManager().setSharedPreferencesName("ActionOnJunkCall");
	            
	            findViews();
	        }
	   
	   private void findViews() {
		// TODO Auto-generated method stub
		blacklist =  ((CheckBoxPreference) findPreference("blacklist"));
		userlist = ((CheckBoxPreference) findPreference("userlist"));
		 junkcall = ((ListPreference)findPreference("junkcall"));
		 suspcall =   ((ListPreference)findPreference("suspcall"));
		 start3 =((CheckBoxPreference) findPreference("3."));
		 start2 =((CheckBoxPreference) findPreference("2."));
		 no_num = ((CheckBoxPreference) findPreference("no_num"));
		 duration = ((CheckBoxPreference) findPreference("duration"));
		 
		 myprefs = getSharedPreferences("ActionOnJunkCall", 0);
		
	}

	@Override
	    protected void onStart() {
	        super.onStart();
	        getPrefsValue();
	        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	    }

	
	@Override
    protected void onPause() {
        super.onPause();

        // Unregister the listener whenever a key changes            
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);    
    }
	   
	   protected void getPrefsValue(){ // initiallization for preference page
		  
		   //junk call
		   blacklist.setChecked(myprefs.getBoolean("blacklist", true));
		   userlist.setChecked(myprefs.getBoolean("userlist", true));

		   	junkcall.setSummary(myprefs.getString("junkcall", "No Actions"));
	        junkcall.setValue(myprefs.getString("junkcall", "No Actions"));
	        
	        
	       //susp call       
	        start3.setChecked(myprefs.getBoolean("3.", true));
	        start2.setChecked(myprefs.getBoolean("2.", true));
	        no_num.setChecked(myprefs.getBoolean("no_num", true));
	        duration.setChecked(myprefs.getBoolean("duration", true));
	        
	        
	        suspcall.setSummary(myprefs.getString("suspcall", "No Actions"));
	        suspcall.setValue(myprefs.getString("suspcall", "No Actions"));
	       
	   }
	   
	 

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
		// TODO Auto-generated method stub
		
        if (key.equals("suspcall")) {
            suspcall.setSummary(myprefs.getString("suspcall", ""));
          }
          else if (key.equals("junkcall")) {
            junkcall.setSummary(myprefs.getString("junkcall", "")); 
          }
		
	}
}
